package com.share.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.PowerBank;
import com.share.device.service.PowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "充电宝接口")
@RestController
@RequestMapping("/powerBank")
public class PowerBankController extends BaseController {
    @Autowired
    private PowerBankService powerBankService;

    /**
     * 获取充电宝列表
     * @param powerBank
     * @return
     */
    @RequestMapping("/list")
    @Operation(summary = "获取充电宝列表")
    public TableDataInfo list(PowerBank powerBank) {
        startPage();
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        return getDataTable(list);
    }

    /**
     * 添加充电宝
     * @param powerBank
     * @return
     */
    @PostMapping
    @Operation(summary = "添加充电宝")
    public AjaxResult add(@Validated @RequestBody PowerBank powerBank) {
        //判断充电宝编号是否已存在
        List<PowerBank> powerBanks = exitPowerBankNo(powerBank);
        if(powerBanks.size() > 0){
            this.error("充电宝编号已存在");
        }
        return toAjax(powerBankService.save(powerBank));
    }
    /**
     * 修改充电宝
     * @param powerBank
     * @return
     */
    @PutMapping
    @Operation(summary = "修改充电宝")
    @Transactional
    public AjaxResult edit(@RequestBody PowerBank powerBank) {
        //判断充电宝编号是否已存在
        List<PowerBank> powerBanks = exitPowerBankNo(powerBank);
        if (powerBanks.size() > 0) {
            this.error("充电宝编号已存在");
        }
        //如果充电宝的状态为已租用，则不能修改
        PowerBank powerBank1 = powerBankService.getById(powerBank.getId());
        if (powerBank1.getStatus().equals("2")) {
            this.error("充电宝已租用，不能修改状态");
        }
        //更新时间
        powerBank.setUpdateTime(new Date());
        return toAjax(powerBankService.updateById(powerBank));
    }

    /**
     * 删除充电宝
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除充电宝")
    public AjaxResult delete(@PathVariable @Schema(description = "充电宝ID", required = true) Long id) {
        return toAjax(powerBankService.removeById(id));
    }

    /**
     * 查询充电宝详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询充电宝详情")
    public PowerBank detail(@PathVariable @Schema(description = "充电宝ID", required = true) Long id) {
        return powerBankService.getById(id);
    }

    public List<PowerBank> exitPowerBankNo(PowerBank powerBank){
        //判断充电宝编号是否已存在
        LambdaQueryWrapper<PowerBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo());
        return powerBankService.list(wrapper);
    }

    /**
     * 获取未投放的充电宝
     * @return
     */
    @GetMapping("/getNoUseList")
    @Operation(summary = "获取未投放的充电宝")
    public List<PowerBank> getNoUseList() {
        LambdaQueryWrapper<PowerBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PowerBank::getStatus, "0");
        return powerBankService.list(wrapper);
    }

}
